package team.pikachu.pikachuclient.message;

import android.content.Context;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import team.pikachu.pikachuclient.beans.Location;
import team.pikachu.pikachuclient.beans.School;
import team.pikachu.pikachuclient.login.SchoolAdapter;

/**
 * Created by Alex on 2017/9/18.
 */

public class LocationAdapter extends ArrayAdapter<Location> implements Filterable{
    private LocationAdapter.ArrayFilter mFilter;
    private List<Location> mList;
    private Context context;
    private ArrayList<Location> mUnfilteredData;
    private CompleteListener mCompleteListener;
    private String text;

    public LocationAdapter(@NonNull Context context, @LayoutRes int resource, @NonNull List<Location> objects) {
        super(context, resource, objects);
        this.mList = objects;
        this.context = context;
    }

    @Override
    public int getCount() {
        return mList == null ? 0 : mList.size();
    }

    @Override
    public Location getItem(int position) {
        // TODO Auto-generated method stub
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view;
        LocationAdapter.ViewHolder holder;
        if (convertView == null) {
            view = View.inflate(context, android.R.layout.simple_list_item_1, null);
            holder = new LocationAdapter.ViewHolder();
            holder.tv_name = (TextView) view.findViewById(android.R.id.text1);
            view.setTag(holder);
        } else {
            view = convertView;
            holder = (LocationAdapter.ViewHolder) view.getTag();
        }
        Location Location = mList.get(position);
        holder.tv_name.setText(Location.getLocation());
        return view;
    }

    static class ViewHolder {
        public TextView tv_name;
    }

    @Override
    public Filter getFilter() {
        if (mFilter == null) {
            mFilter = new LocationAdapter.ArrayFilter();
        }
        return mFilter;
    }

    private class ArrayFilter extends Filter {

        @Override
        protected FilterResults performFiltering(CharSequence prefix) {
            text = prefix.toString();

            FilterResults results = new FilterResults();

            if (mUnfilteredData == null) {
                mUnfilteredData = new ArrayList<>(mList);
            }

            if (prefix == null || prefix.length() == 0) {
                ArrayList<Location> list = mUnfilteredData;
                results.values = list;
                results.count = list.size();
            } else {
                String prefixString = prefix.toString().toLowerCase();

                ArrayList<Location> unfilteredValues = mUnfilteredData;
                int count = unfilteredValues.size();

                ArrayList<Location> newValues = new ArrayList<Location>(count);

                for (int i = 0; i < count; i++) {
                    Location pc = unfilteredValues.get(i);
                    if (pc != null) {

                        if(pc.getLocation()!=null && pc.getLocation().startsWith(prefixString)){

                            newValues.add(pc);
                        }else if(pc.getLocation()!=null && pc.getLocation().startsWith(prefixString)){

                            newValues.add(pc);
                        }
                    }
                }
                results.values = newValues;
                results.count = newValues.size();
            }

            return results;
        }

        @Override
        protected void publishResults(CharSequence constraint,
                                      FilterResults results) {
            //noinspection unchecked
            mList = (List<Location>) results.values;
            if (results.count == 1) {
                Location school = mList.get(0);
                if (school.getLocation().equals(text)) {
                    mCompleteListener.onComplete(school);
                }
            }

            if (results.count > 0) {
                notifyDataSetChanged();
            } else {
                notifyDataSetInvalidated();
            }
        }

    }

    public interface CompleteListener {
        void onComplete(Location location);
    }

    public void setOnCompleteListener(CompleteListener listener) {
        this.mCompleteListener = listener;
    }
}
